using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JVIT.TimeSheet.BLL
{
    [Serializable]
    public class TimedObject
    {
        public const string SHORT_TIMESTAMP_FORMAT = "{2:0} seconds";
        public const string MEDIUM_TIMESTAMP_FORMAT = "{1:0} minutes + {2:0} seconds";
        public const string LONG_TIMESTAMP_FORMAT = "{0:0} hours + {1:0} minutes + {2:0} seconds";

        public string From { get { return ""; } }
        public string To { get { return ""; } }

        public TimeSpan Duration
        {
            get
            {
                if (this.Started.HasValue == false || this.Ended.HasValue == false) return new TimeSpan();
                else return this.Ended.Value - this.Started.Value;
            }
        }

        public TimeSpan DurationUnfinished
        {
            get
            {
                if (this.Ended.HasValue)
                    return this.Duration;
                else
                    return this.Duration + (DateTime.Now - this.Started.Value);
            }
        }

        public DateTime Created { get; set; }
        public DateTime LastUpdate { get; set; }
        private DateTime? _started;
        public DateTime? Started { get { return _started; } set { _started = value; this.ApplyChange(); } }
        private DateTime? _ended;
        public DateTime? Ended { get { return _ended; } set { _ended = value; this.ApplyChange(); } }
        public void ApplyChange()
        {
            if (this.Created == null) this.Created = DateTime.Now;
            this.LastUpdate = DateTime.Now;
        }
        public void Start()
        {
            this.Started = DateTime.Now;
        }
        public void Stop()
        {
            this.Ended = DateTime.Now;
        }
    }
}
